<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>

      <template #status="{ record }">
        <div v-if="record.payStatus==0">
          <Tag color="orange">待支付</Tag>
        </div>
        <div v-if="record.payStatus==1">
          <Tag color="green">已支付</Tag>
        </div>
        <div v-if="record.payStatus==2">
          <Tag color="purple">已退款</Tag>
        </div>
        <div v-if="record.payStatus==3" :title="record.refundResultCode">
          <Tag color="red">退款失败</Tag>
        </div>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <CsCoursePayOrderDrawer @register="registerDrawer" @success="handleSuccess"></CsCoursePayOrderDrawer>
  </div>
</template>

<script lang="ts" name="cscoursepayorder-csCoursePayOrder" setup>
  import {reactive} from 'vue';
  import { Tag} from 'ant-design-vue';
  import {BasicTable, TableAction} from '/@/components/Table';
   import { useDrawer } from '/@/components/Drawer';
  import { useListPage } from '/@/hooks/system/useListPage'
  import CsCoursePayOrderDrawer from './components/CsCoursePayOrderDrawer.vue'
  import {columns, searchFormSchema} from './CsCoursePayOrder.data';
  import {list,refund} from './CsCoursePayOrder.api';
  const queryParam = reactive<any>({});
  //注册model
  const [registerDrawer, {openDrawer}] = useDrawer();
  //注册table数据
  const { tableContext} = useListPage({
      tableProps:{
           title: '课程缴费单',
           api: list,
           columns,
           canResize:false,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
                 ['payTime', ['payTime_begin', 'payTime_end'], 'YYYY-MM-DD'],
              ],
            },
           actionColumn: {
               width: 120,
               fixed:'right'
            },
            beforeFetch: (params) => {
              return Object.assign(params, queryParam);
            },
      }
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext


   /**
    * 详情
   */
  function handleDetail(record: Recordable) {
      openDrawer(true, {
       id: record.id,
       showFooter: true,
       name: record.name,
     });
   }

   function handleDelete(record: Recordable) {
    refund({
        id: record.id
     },handleSuccess);
   }
  
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
      let menu = new Array();
      menu.push({
           label: '详情',
           onClick: handleDetail.bind(null, record),
      });
      if(record.payStatus==1){
        menu.push({
           label: '退课',
           onClick: handleDelete.bind(null, record),
         });
      }
      return menu;
   }
    


</script>

<style scoped>

</style>